package com.xbn.xbnmall.coupon.service.impl;

import com.xbn.common.to.MemberPrice;
import com.xbn.common.to.SkuReductionTo;
import com.xbn.xbnmall.coupon.entity.MemberPriceEntity;
import com.xbn.xbnmall.coupon.entity.SkuLadderEntity;
import com.xbn.xbnmall.coupon.service.MemberPriceService;
import com.xbn.xbnmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.Query;

import com.xbn.xbnmall.coupon.dao.SkuFullReductionDao;
import com.xbn.xbnmall.coupon.entity.SkuFullReductionEntity;
import com.xbn.xbnmall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }
    //ku的优惠、满减等信息 操作xbnmall_sms数据库中的折扣表sms_sku_ladder
//        以及满减表sms_sku_full_reduction还有会员价格表sms_member_price
    @Override
    public void saveSkuRelation(SkuReductionTo skuReductionTo) {
        //1、保存满减打折的信息
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuLadderEntity.getDiscount());
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        //判断满减打折数据是否有意义()不为0，无意义则不需要保存
        if(skuReductionTo.getFullCount()>0)
        {
            skuLadderService.save(skuLadderEntity);
        }


        //2、满减信息sms_sku_full_reduction
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,fullReductionEntity);
        //满减达标数额以及减少的金额都大于0才执行保存
        if(fullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1
        &&fullReductionEntity.getReducePrice().compareTo(new BigDecimal("0"))==1) {
            this.save(fullReductionEntity);
        }

        //3、会员价格sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        })
                //过滤掉会员价格为0.00（没有设置）的会员价格数据
                .filter(item->{
                   return item.getMemberPrice().compareTo(new BigDecimal("0"))==1;
                })
                .collect(Collectors.toList());
        memberPriceService.saveBatch(collect);

    }

}